import os, xml.etree.ElementTree as ET


def convert_annotation(xml_file, out_txt):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    w = int(root.find('size/width').text)
    h = int(root.find('size/height').text)
    res = []
    for obj in root.findall('object'):
        if obj.find('name').text != 'person': continue
        bb = obj.find('bndbox')
        xmin, ymin = float(bb.find('xmin').text), float(bb.find('ymin').text)
        xmax, ymax = float(bb.find('xmax').text), float(bb.find('ymax').text)
        x_c = (xmin + xmax) / 2 / w
        y_c = (ymin + ymax) / 2 / h
        bw = (xmax - xmin) / w
        bw = (ymax - ymin) / h
        res.append(f"0 {x_c:.6f} {y_c:.6f} {bw:.6f} {(ymax - ymin) / h:.6f}")
    open(out_txt, 'w').write("\n".join(res))
